﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
public partial class JobManage_PendingJobList : BasePage
{
    DataBase db = new DataBase();
    IJobHandler jobManage = new JobHandler();
    ISyClientHandler clientManage = new SyClientHandler();
    IRolePurviewHandler userManage = new RolePurviewHandler();
    ISystemHandler systemManage = new SystemHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1061")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }              
            }

            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "desc";
            dataBind();            
        }
    }
    
   
    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";
        UsersQuery userQuery = userManage.GetUsers(int.Parse(Session["UserID"].ToString()));
        if (userQuery != null)
        {

            list.Add(new SqlParameter("@Contractor", userQuery.ClientID.ToString()));
            sqlstr += " and Contractor=@Contractor ";

            if (ViewState["Value"] != null)
            {
                if (ViewState["Name"].ToString().ToLower() == "vendorid")
                {
                    if (ViewState["Value"].ToString().EndsWith("%"))
                    {
                        list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                        sqlstr += " and " + ViewState["Name"].ToString() + " in(select PKID from Sy_Client where CNShortName like  @ColumnName + '%'  or ENGShortName like  @ColumnName + '%' or HKShortName like @ColumnName + '%')";

                    }
                    else
                    {
                        list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                        sqlstr += " and " + ViewState["Name"].ToString() + " in(select PKID from Sy_Client where CNShortName = @ColumnName  or ENGShortName  @ColumnName or HKShortName = @ColumnName)";
 
                    }
                }
                else
                {
                    if (ViewState["Value"].ToString().EndsWith("%"))
                    {
                        list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                        sqlstr += " and " + ViewState["Name"].ToString() + " like @ColumnName + '%'";
                    }
                    else
                    {
                        list.Add(new SqlParameter("@ColumnName", ViewState["Value"].ToString()));
                        sqlstr += " and " + ViewState["Name"].ToString() + "=@ColumnName";
                    }
                }
            }
            if (ViewState["Value2"] != null && ViewState["Value3"] != null)
            {
                list.Add(new SqlParameter("@DateFrom", ViewState["Value2"].ToString().ToString()));
                list.Add(new SqlParameter("@DateTo", ViewState["Value3"].ToString()));
                sqlstr += " and " + ViewState["Name2"].ToString() + " between @DateFrom and @DateTo ";
            }
            else if (ViewState["Value2"] != null && ViewState["Value3"] == null)
            {
                list.Add(new SqlParameter("@DateFrom", ViewState["Value2"].ToString().ToString()));
                sqlstr += " and " + ViewState["Name2"].ToString() + " >= @DateFrom ";
            }
            else if (ViewState["Value2"] == null && ViewState["Value3"] != null)
            {
                list.Add(new SqlParameter("@DateFrom", ViewState["Value3"].ToString().ToString()));
                sqlstr += " and " + ViewState["Name2"].ToString() + " <= @DateFrom ";
            }

            list.Add(new SqlParameter("@Status", Enums.ShippingOrderStatus.NEW.ToString("d")));
            sqlstr += " and Status = @Status";

            parms = list.ToArray();
        }

    }

    private void dataBind()
    {
        if (Session["UserID"] != null)
        {
            string sqlStr = @"SELECT *,
                        (select CNShortName from Sy_Client as SC where SC.PKID=BN.VendorID) as VendorCNShortName ,
                        (select ENGShortName from Sy_Client as SC where SC.PKID=BN.VendorID) as VendorENGShortName,
                        (select HKShortName from Sy_Client as SC where SC.PKID=BN.VendorID) as VendorHKShortName
                        FROM BN_ShippingOrder as BN where 1=1 ";
            SqlParameter[] parms = null;
            string clause = string.Empty;
            ProcessParms(ref clause, ref parms);
            sqlStr += clause;

            DataTable dt = jobManage.GetShippingOrder(sqlStr, parms);
            DataView view = dt.DefaultView;
            view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
            gvJobList.DataSource = view;
            gvJobList.DataBind();
            if (dt != null && dt.Rows.Count > 0)
            {
                lblTotal.Text = dt.Rows.Count.ToString();
            }
            else
            {
                lblTotal.Text = "0";
            }
        }
    }
    protected void gvJobList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvJobList.PageIndex = e.NewPageIndex;
        dataBind();

    }
    protected void gvJobList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvJobList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtName.Text != string.Empty)
        {
            ViewState["Value"] = txtName.Text.Trim();
            ViewState["Name"] = ddlColumnName.SelectedValue;
        }
        else
        {
            ViewState["Value"] = null;
            ViewState["Name"] = null;
        }

        if (txtBetween.Text != string.Empty && txtTo.Text != string.Empty)
        {
            ViewState["Value2"] = txtBetween.Text.Trim() + " 00:00:00";
            ViewState["Name2"] = ddlColumnName2.SelectedValue;
            ViewState["Value3"] = txtTo.Text.Trim() + " 23:59:59";
        }
        else if (txtBetween.Text != string.Empty && txtTo.Text == string.Empty)
        {
            ViewState["Value2"] = txtBetween.Text.Trim() + " 00:00:00";
            ViewState["Name2"] = ddlColumnName2.SelectedValue;
            ViewState["Value3"] = null;
        }
        else if (txtBetween.Text == string.Empty && txtTo.Text != string.Empty)
        {
            ViewState["Value2"] = null;
            ViewState["Name2"] = ddlColumnName2.SelectedValue;
            ViewState["Value3"] = txtTo.Text.Trim() + " 00:00:00";
        }
        else
        {
            ViewState["Value2"] = null;
            ViewState["Name2"] = null;
            ViewState["Value3"] = null;
        }      

        dataBind();
    }


    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvJobList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvJobList.PageCount)
            {
                this.gvJobList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.Alert(Resources.Messages.WrongPageNumInput, this.Page);
                return;
            }
        }
        catch
        {
            Jscript.Alert(Resources.Messages.WrongPageNumInput, this.Page);

        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvJobList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }

    public string GetClient(object id)
    {
        return systemManage.GetClient(id);
    }

    public string GetUserName(object userID)
    {
        return systemManage.GetUserName(userID);
    }
}
